package demo1.Frames;

import demo1.Panels.MySinLine;
import demo1.Panels.MyPicture;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class MyPanels extends JFrame {
    JPanel root = new JPanel();
    MySinLine sinLine = new MySinLine();
    MyPicture picture = new MyPicture();

    public MyPanels(String title) {
        super(title);
        this.setContentPane(root);
        SinLine();
        //Picture();
    }

    public void Picture() {
        root.add(picture);
        picture.setPreferredSize(new Dimension(300, 150));
    }

    public void SinLine() {
        root.add(sinLine);
        sinLine.setPreferredSize(new Dimension(300, 150));
        sinLine.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton() == MouseEvent.BUTTON3) {
                    showMenu(e);
                }
            }
        });
    }

    private void showMenu(MouseEvent e) {
        JPopupMenu menu = new JPopupMenu();

        JLabel label = new JLabel("参数设置");
        menu.add(label);

        JMenuItem item1 = new JMenuItem("sin");
        item1.addActionListener(new ItemListener());
        item1.setActionCommand("sin");
        menu.add(item1);

        JMenuItem item2 = new JMenuItem("cos");
        item2.addActionListener(new ItemListener());
        item2.setActionCommand("cos");
        menu.add(item2);

        JMenuItem item3 = new JMenuItem("tan");
        item3.addActionListener(new ItemListener());
        item3.setActionCommand("tan");
        menu.add(item3);

        menu.show(e.getComponent(),e.getX(),e.getY());
    }

    private static class ItemListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(e.getActionCommand().equals("sin")) {
                System.out.println("sin");
            }
            if(e.getActionCommand().equals("cos")) {
                System.out.println("cos");
            }
            if(e.getActionCommand().equals("tan")) {
                System.out.println("tan");
            }
        }
    }
}
